package il.ac.braude.sona.gui.forms.myforms;

import il.ac.braude.sona.entities.ExtensionRequest;
import il.ac.braude.sona.gui.forms.FormFrame;
import il.ac.braude.sona.gui.forms.FormPanel;
import il.ac.braude.sona.gui.forms.questions.QComboBox;
import il.ac.braude.sona.gui.forms.questions.QTextArea;
import il.ac.braude.sona.gui.forms.questions.QTextField;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * The ExtensionRequestsApproveForm is a FormFrame that create the gui for approve extension request
 *  
 * @author Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 */
public class ExtensionRequestsApproveForm extends FormFrame {
	/**
	 * panel - the panel for approve and decline buttons
	 */
	private FormPanel panel;
	/**
	 * textpanel - the panel for displaying extension request information
	 */
	private FormPanel textpanel;
	/**
	 * buttonpanel - the panel contains approve and decline buttons 
	 */
	public JPanel buttonpanel;
	/**
	 * reqid - list of request ids that asking for extension 
	 */
	private String[] reqid;
	/**
	 * box - QComboBox of request ids that asking for extension 
	 */
	private QComboBox box;
	/**
	 * rid - QTextField of request id
	 */
	private QTextField rid;
	/**
	 * sid - QTextField of request status
	 */
	private QTextField sid;
	/**
	 * owner - QTextField of request owner
	 */
	private QTextField owner;
	/**
	 * days - QTextField of requested days extension
	 */
	private QTextField days;
	/**
	 * exp - QTextArea of extension explenation
	 */
	private QTextArea exp;
	/**
	 * requestCount - number of requested extensions
	 */
	public int requestCount;


	/**
	 * Constructs the ExtensionRequestsApproveForm.
	 * initiate the GUI for User
	 * 
	 * @param title -title of the form
	 * @param approveButton - the formPanel answer button
	 * @param values - ArrayList contains all information about Extension Requests
	 */
	public ExtensionRequestsApproveForm(String title, String approveButton,ArrayList<ExtensionRequest> values) {
		super(title, approveButton);
		int i = 0;
		requestCount = values.size();
		panel = new FormPanel();
		textpanel = new FormPanel();
		textpanel.setLayout(new BoxLayout(textpanel,BoxLayout.Y_AXIS));
		panel.setLayout(new BorderLayout());
		
		buttonpanel = new JPanel();
		buttonpanel.setOpaque(false);

		rid = new QTextField("R.ID: ");
		sid = new QTextField("Status: ");
		owner = new QTextField("Owner: ");
		days = new QTextField("Days#: ");
		exp = new QTextArea("Explanation: ");
		reqid = new String[values.size()];
		
		for (ExtensionRequest e:values)
			reqid[i++] = e.getValue("rid");

		box = new QComboBox("Pending Requests:",reqid);
		box.setDefaultAnswer(0);
		panel.add(box,BorderLayout.NORTH);

		rid.setDefaultAnswer(values.get(0).getValue("rid"));
		rid.setEditable(false);
		sid.setDefaultAnswer(values.get(0).getValue("sid"));
		sid.setEditable(false);
		owner.setDefaultAnswer(values.get(0).getValue("uid"));
		owner.setEditable(false);
		days.setDefaultAnswer(values.get(0).getValue("extension_days"));
		days.setEditable(false);
		exp.setDefaultAnswer(values.get(0).getValue("explanation"));
		exp.setEditable(false);
		panel.add(buttonpanel,BorderLayout.SOUTH);

		textpanel.addQuestion(rid);
		textpanel.addQuestion(sid);
		textpanel.addQuestion(owner);
		textpanel.addQuestion(days);
		textpanel.addQuestion(exp);

		panel.add(textpanel,BorderLayout.WEST);

		setFormPanel(panel);
	}
	/**
	 * returns the field box
	 * @return box
	 */
	public QComboBox getBox() {
		return box;
	}
	/**
	 * sets the field box
	 * @param box
	 */
	public void setBox(QComboBox box) {
		this.box = box;
	}
	/**
	 * returns the field panel
	 * @return panel
	 */
	public FormPanel getPanel() {
		return panel;
	}
	/**
	 * sets the field panel
	 * @param panel
	 */
	public void setPanel(FormPanel panel) {
		this.panel = panel;
	}
	/**
	 * add buttons to buttonpanel
	 * @param title - the button title
	 * @param listener - the button listener
	 */
	public void addButton(String title,ActionListener listener) {
		JButton button = new JButton(title);
		button.addActionListener(listener);
		buttonpanel.add(button);
	}
	/**
	 * sets the deafault answer for ExtensionRequest form
	 * @param e - the ExtensionRequest
	 */
	public void changeAnswer(ExtensionRequest e)
	{
		rid.setDefaultAnswer(e.getValue("rid"));
		sid.setDefaultAnswer(e.getValue("sid"));
		owner.setDefaultAnswer(e.getValue("uid"));
		days.setDefaultAnswer(e.getValue("extension_days"));
		exp.setDefaultAnswer(e.getValue("explanation"));
	}


}
